package com.kaibes.web.thymeleaf;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Component
@Order(1)
public class CacheHandlerInterceptor implements HandlerInterceptor {

    @Value("${kaibes.web.thymeleaf.use-cache:true}")
    private boolean useCache;
    
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        if (!useCache) {
            response.setHeader("Cache-Control","no-cache, no-store, must-revalidate"); 
            response.setHeader("Pragma","no-cache"); 
            response.setDateHeader("Expires",0); 
        }
        return HandlerInterceptor.super.preHandle(request, response, handler);
    }
    
}
